package com.bluefox.modules.quartz.config;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;
import org.springframework.stereotype.Component;

/**
 * @author 孙峰
 * @Description: 任务工厂注入到Spring 解决Job中注入Spring Bean为null的问题
 * @date 2020/12/22 11:11
 */
@Component("quartzJobFactory")
public class QuartzJobFactory extends SpringBeanJobFactory {

    private final AutowireCapableBeanFactory capableBeanFactory;

    public QuartzJobFactory(AutowireCapableBeanFactory capableBeanFactory) {
        this.capableBeanFactory = capableBeanFactory;
    }

    @Override
    protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {

        //调用父类的方法
        final  Object jobInstance = super.createJobInstance(bundle);
        capableBeanFactory.autowireBean(jobInstance);
        return jobInstance;
    }
}
